#include "high_ntc.h"

#if 0
//-40~150
const uint16_t temp_ntc_table[TEMP_NTC_LEN] = {4031, 4027, 4023, 4018, 4013, 4007, 4002, 3996,
                                               3989, 3983, 3976, 3969, 3961, 3953, 3945, 3936,
                                               3927, 3918, 3908, 3897, 3886, 3875, 3863, 3850,
                                               3837, 3824, 3809, 3794, 3779, 3763, 3746, 3729,
                                               3710, 3691, 3672, 3652, 3631, 3609, 3586, 3563,
                                               3539, 3514, 3489, 3463, 3436, 3408, 3379, 3350,
                                               3320, 3289, 3258, 3226, 3193, 3160, 3126, 3091,
                                               3055, 3019, 2983, 2946, 2908, 2870, 2831, 2792,
                                               2752, 2713, 2672, 2632, 2591, 2550, 2508, 2467,
                                               2425, 2384, 2342, 2300, 2258, 2216, 2175, 2133,
                                               2091, 2050, 2009, 1968, 1928, 1887, 1847, 1808,
                                               1769, 1730, 1692, 1654, 1616, 1579, 1543, 1507,
                                               1472, 1437, 1403, 1369, 1336, 1303, 1271, 1240,
                                               1209, 1179, 1150, 1121, 1092, 1064, 1037, 1011,
                                               985, 959, 935, 910, 887, 864, 841, 819, 798,
                                               777, 756, 736, 717, 698, 680, 662, 644, 627,
                                               611, 594, 579, 563, 549, 534, 520, 506, 493,
                                               480, 467, 455, 443, 432, 420, 409, 399, 388,
                                               378, 368, 359, 350, 341, 332, 323, 315, 307,
                                               299, 292, 284, 277, 270, 263, 257, 250, 244,
                                               238, 232, 226, 221, 215, 210, 205, 200, 195,
                                               190, 186, 181, 177, 173, 169, 165, 161, 157,
                                               153, 150, 146, 143, 139, 136, 133};
#endif

#if 0
//-40~150 12k
const uint16_t temp_ntc_table[TEMP_NTC_LEN] = {3855, 3843, 3830, 3816, 3802, 3787, 3772, 3756,
                                               3740, 3723, 3705, 3686, 3667, 3648, 3627, 3606,
                                               3584, 3562, 3539, 3515, 3491, 3465, 3440, 3413,
                                               3386, 3358, 3329, 3300, 3270, 3239, 3207, 3175,
                                               3143, 3110, 3076, 3044, 3007, 2971, 2935, 2899,
                                               2860, 2822, 2785, 2747, 2708, 2665, 2626, 2586,
                                               2547, 2507, 2463, 2423, 2383, 2343, 2302, 2258,
                                               2218, 2178, 2139, 2099, 2056, 2017, 1978, 1939,
                                               1900, 1862, 1827, 1789, 1752, 1715, 1672, 1636,
                                               1600, 1565, 1531, 1495, 1462, 1429, 1396, 1364,
                                               1332, 1301, 1271, 1241, 1212, 1182, 1154, 1127,
                                               1100, 1073, 1047, 1022, 997, 973, 949, 926, 903,
                                               881, 860, 838, 818, 797, 778, 758, 740, 722,
                                               704, 686, 669, 653, 637, 621, 605, 590, 576,
                                               562, 548, 534, 521, 509, 496, 484, 472, 460,
                                               449, 442, 428, 417, 407, 397, 388, 379, 369,
                                               360, 352, 344, 336, 328, 320, 310, 305, 298,
                                               291, 284, 278, 271, 266, 260, 254, 248, 242,
                                               237, 232, 226, 221, 216, 211, 206, 202, 198,
                                               193, 189, 185, 181, 177, 173, 169, 166, 162,
                                               158, 155, 152, 149, 146, 142, 139, 137, 134,
                                               131, 128, 125, 123, 121, 118, 116, 113, 111,
                                               109, 107, 104, 102};
#endif
//-40~150 12k new
const uint16_t temp_ntc_table[TEMP_NTC_LEN] = {3956, 3947, 3937, 3926, 3915, 3904, 3892, 3879,
                                               3865, 3851, 3836, 3821, 3804, 3787, 3769, 3751,
                                               3731, 3711, 3690, 3668, 3645, 3621, 3596, 3570,
                                               3543, 3516, 3487, 3458, 3427, 3396, 3363, 3330,
                                               3296, 3261, 3225, 3188, 3150, 3112, 3072, 3032,
                                               2991, 2949, 2907, 2865, 2822, 2778, 2734, 2689,
                                               2644, 2599, 2553, 2507, 2461, 2414, 2368, 2321,
                                               2274, 2228, 2181, 2135, 2089, 2043, 1997, 1951,
                                               1907, 1862, 1818, 1774, 1731, 1688, 1646, 1604,
                                               1563, 1523, 1483, 1444, 1406, 1368, 1331, 1295,
                                               1259, 1225, 1191, 1158, 1125, 1093, 1060, 1030,
                                               1000, 971, 943, 915, 888, 863, 837, 813, 789,
                                               765, 743, 721, 700, 679, 659, 639, 620, 602,
                                               584, 567, 550, 534, 518, 503, 489, 474, 460,
                                               447, 434, 421, 409, 397, 386, 375, 364, 353,
                                               343, 334, 324, 315, 306, 297, 289, 280, 273,
                                               265, 257, 250, 243, 237, 230, 224, 218, 212,
                                               206, 201, 195, 190, 185, 180, 175, 171, 166,
                                               162, 158, 154, 150, 146, 142, 139, 135, 132,
                                               129, 125, 122, 119, 116, 114, 111, 108, 105,
                                               103, 101, 98, 96, 94, 92, 89, 88, 86, 84, 82,
                                               80, 78, 76, 74, 73, 71, 69, 68, 66, 65, 64};

#if 0
//-40~150 10k
const uint16_t temp_ntc_table[TEMP_NTC_LEN] = {3893, 3883, 3872, 3860, 3848, 3835, 3822, 3809,
                                               3795, 3780, 3765, 3749, 3732, 3715, 3698, 3680,
                                               3661, 3641, 3621, 3600, 3579, 3557, 3534, 3510,
                                               3486, 3462, 3436, 3410, 3383, 3356, 3328, 3299,
                                               3270, 3240, 3209, 3180, 3146, 3114, 3081, 3047,
                                               3011, 2977, 2942, 2906, 2870, 2830, 2793, 2756,
                                               2718, 2680, 2638, 2600, 2561, 2522, 2484, 2441,
                                               2402, 2363, 2324, 2285, 2242, 2203, 2164, 2125,
                                               2087, 2048, 2013, 1975, 1937, 1899, 1855, 1818,
                                               1781, 1745, 1709, 1672, 1637, 1603, 1569, 1535,
                                               1500, 1468, 1436, 1404, 1373, 1341, 1311, 1281,
                                               1252, 1224, 1196, 1168, 1141, 1114, 1088, 1063,
                                               1038, 1014, 990, 966, 944, 921, 899, 878, 857,
                                               836, 816, 797, 778, 759, 741, 723, 706, 689,
                                               672, 656, 640, 625, 610, 595, 581, 567, 553,
                                               540, 527, 519, 503, 491, 479, 468, 457, 446,
                                               435, 425, 415, 406, 396, 387, 378, 366, 361,
                                               352, 344, 336, 329, 321, 315, 308, 301, 294,
                                               287, 281, 275, 268, 262, 256, 251, 245, 240,
                                               235, 229, 225, 220, 215, 210, 205, 201, 197,
                                               193, 189, 185, 181, 177, 173, 170, 166, 163,
                                               159, 156, 153, 150, 147, 144, 141, 138, 135,
                                               132, 130, 127, 125, 122};
#endif
//-40~125
const uint16_t PCB_NTC_table[PCB_NTC_LEN] = {3890, 3879, 3867, 3856, 3843, 3831, 3817, 3803,
                                             3789, 3774, 3758, 3742, 3726, 3708, 3690, 3672,
                                             3653, 3633, 3612, 3591, 3569, 3547, 3524, 3500,
                                             3476, 3451, 3425, 3398, 3371, 3343, 3315, 3286,
                                             3256, 3226, 3195, 3164, 3131, 3099, 3066, 3032,
                                             2997, 2963, 2927, 2892, 2856, 2819, 2782, 2745,
                                             2707, 2669, 2631, 2593, 2554, 2515, 2476, 2438,
                                             2399, 2360, 2321, 2281, 2242, 2203, 2164, 2125,
                                             2086, 2048, 2010, 1972, 1934, 1896, 1859, 1822,
                                             1786, 1750, 1714, 1678, 1643, 1609, 1575, 1541,
                                             1508, 1475, 1443, 1411, 1380, 1349, 1319, 1289,
                                             1260, 1231, 1203, 1175, 1148, 1122, 1095, 1070,
                                             1045, 1020, 996, 973, 950, 927, 905, 884, 863,
                                             842, 822, 802, 783, 765, 746, 728, 711, 694, 677,
                                             661, 645, 630, 614, 600, 586, 572, 558, 545, 532,
                                             519, 507, 495, 483, 472, 461, 450, 439, 429, 419,
                                             409, 400, 391, 381, 373, 364, 356, 347, 340, 332,
                                             324, 317, 310, 303, 296, 289, 283, 277, 270, 264,
                                             259, 253, 247, 242, 237, 231, 226, 222, 217, 212,
                                             208};

/*
 * 函数介绍: 查找数据在表中对应的位置    表中数据从小到大
 * 参数: addr ntc表的地址
 * 参数: tab_len 表的长度
 * 参数: data ADC采样值
 * 返回值: {*}
 * 备注:
 */
float ntc_find_tab_big(const uint16_t *addr, uint8_t tab_len, uint16_t data)
{
    float deci = 0;
    float res = 0;
    uint8_t start = 0u;
    uint8_t end = tab_len - 1u;
    uint8_t result = 0u;
    uint8_t i = 0u;

    if (data >= addr[end])
    {
        return end;
    }
    else if (data <= addr[start])
    {
        return start;
    }

    while (start <= end)
    {
        result = (start + end) / 2u;

        if (data == addr[result])
        {
            break;
        }
        if (data < addr[result + 1] && data > addr[result])
        {
            break;
        }


        if (data > addr[result])
        {
            start = result;  //end = result ;
        }
        else
        {
            end = result;
        }//start = result ;

        if (i++ > tab_len)
        {
            break;
        }
    }

    if (start > end)
    {
        res = 0;
    }
    else
    {
        if (addr[result] != data)
        {
            deci = (addr[result + 1] - addr[result]) / 10.0;
            res = ((addr[result + 1] - data) / deci) + result;
        }
        else
        {
            res = result;
        }
    }

    return res;
}

/*
 * 函数介绍: 查找数据在表中对应的位置 表中数据从大到小
 * 参数: addr ntc表的地址
 * 参数: tab_len 表的长度
 * 参数: data ADC采样值
 * 返回值: {*}
 * 备注:
 */
float ntc_find_tab_low(const uint16_t *addr, uint8_t tab_len, uint16_t data)
{
    float deci = 0;
    float res = 0;
    uint8_t start = 0u;
    uint8_t end = tab_len - 1u;
    uint8_t result = 0u;
    uint8_t i = 0;
    float  temp_data = 0;


    if (data >= addr[start])
    {
        return start;
    }
    else if (data <= addr[end])
    {
        return end;
    }

    while (start < end)
    {
        result = (end + start) / 2u;

        if (data > addr[result])
        {
            end = result - 1;
        }
        else if (data < addr[result])
        {
            start = result + 1;
        }
        else
        {
            break;
        }


        if (i++ > tab_len)
        {
            break;
        }
    }

#if 1
    if (start > end)
    {
        result = end;
    }
    else
    { ;
    }
#endif
    {
        if (result && addr[result] != data)
        {
            deci = (addr[result] - addr[result + 1]);
            temp_data = addr[result] - data;
            res = ((float) temp_data / deci) + result;
        }
        else
        {
            res = result;
        }
    }

    return res;
}

/*
 * 函数介绍: 获取NTC的温度
 * 参数: ntc 类型选择
 * 参数: ad_value ADC采样值
 * 返回值: 温度 -40开始
 * 备注:
 */
float ntc_get_temp(enum NTC_Typedef ntc, uint16_t ad_value)
{
    float temp = 0u;

    if (ntc == NTC_PCB)
    {
//        ad_value = 1989;
//        ad_value = 2011;
        temp = ntc_find_tab_low(PCB_NTC_table, PCB_NTC_LEN, ad_value);
    }
    else
    {
//        ad_value = 430;
//        ad_value = 167;
//        ad_value = 3048;
        temp = ntc_find_tab_low(temp_ntc_table, TEMP_NTC_LEN, ad_value);
    }
    /*temp -= NTC_OFFSET;*/
    return temp;
}

